package com.eatos.milktea.web;

import com.eatos.milktea.entity.Shops;
import com.eatos.milktea.entity.vo.MyResult;
import com.eatos.milktea.service.ShopsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/shops")
@CrossOrigin
public class ShopsController {
    @Autowired
    private ShopsService shopsService;

    @RequestMapping("/findall")
    public MyResult findAll(){ //将中数据库中返回的结果，封装到MyResult 中
        MyResult myResult = new MyResult();
        try {
            List<Shops> shopsList = shopsService.findAll();
            if (shopsList!=null && shopsList.size()>0){
                myResult.setStatusCode(200);
                myResult.setMessage("已查询门店列表成功");
                myResult.setMydata(shopsList);
            }
            else {
                myResult.setStatusCode(404);
                myResult.setMessage("没有查询门店列表成功");
                myResult.setMydata(null);
            }
        }
        catch (Exception ex){
            System.out.println(ex);
            myResult.setStatusCode(500);
            myResult.setMessage("查询门店列表出错");
            myResult.setMydata(null);
        }

        return myResult;
    }

    @RequestMapping({"/findbycity/{city}","/findbycity"}) //请求路径
    public MyResult findByCity(@PathVariable(value = "city",required = false) String city){
        MyResult myResult = new MyResult();//将查询的数据放到myResult
        try {
            List<Shops> shopsList = shopsService.findByCity(city);
            if (shopsList.size()>0){
                myResult.setStatusCode(200);
                myResult.setMessage("已查询门店列表成功");
                myResult.setMydata(shopsList);
            }
            else {
                myResult.setStatusCode(404);
                myResult.setMessage("没有查询门店列表成功");
                myResult.setMydata(null);
            }
        }
        catch (Exception ex){
            System.out.println(ex);
            myResult.setStatusCode(500);
            myResult.setMessage("查询门店列表出错");
            myResult.setMydata(null);
        }

        return myResult;
    }
}
